<?
if (!defined('IN_RJ')) die("Error 404");
if($cmd[1]=="search" && $cmd[2] && $cmd[4]){
	if (!$cmd[3]) $cmd[3] = 1;
	$limit = ($cmd[3]-1)*$conf['media_per_page'];
	$kw = strtolower($func->utf8_to_ascii(urldecode($cmd[4])));
	if($cmd[2]==1){ //search song
		$q="SELECT * FROM {$conf['prefix']}media WHERE m_title_ascii LIKE '%".$kw."%' ORDER BY m_title ASC, m_title_ascii ASC LIMIT ".$limit.",".$conf['media_per_page'];
		$tt = $DB->num_rows($DB->query("SELECT m_id FROM ".$conf['prefix']."media WHERE m_title_ascii LIKE '%".$kw."%'"));
		$ro= $tpl->get_tpl('list_media');
		if($tt){
			$t['row'] = $tpl->get_block_from_str($ro,'list_media.row',1);
			$i=0;
			$qr=$DB->query($q);
			while($song=$DB->fetch_row($qr)){
				$i++;
				$singer_name=$func->get_data('singer',$song['m_singer']);
				$html .= $tpl->assign_vars($t['row'],
					array(
					'song.TITLE'	=> $song['m_title'],
					'song.TYPE'		=> "<img src='{TPL_LINK}/".$func->get_data('player',$song['m_type'])."' border=0 >",
					'song.ID'		=> $song['m_id'],
					'song.VIEWED'	=> $song['m_viewed'],
					'song.DOWNLOADED' => $song['m_downloaded'],
					'song.URL'		=> $func->change_link('song',$song['m_id'],$func->title_2_ascii($song['m_title_ascii'])),
					'song.SINGER_NAME' => $singer_name,
					'song.SINGER_URL' => $func->change_link('singer',$song['m_singer'],$func->title_2_ascii($singer_name,true)),
					'song.ADD_PL'	=> $logged_in?"select_playlist({$song['m_id']});return false;":"guest_playlist({$song['m_id']});return false;",
					)
				);
				
			}	
		}
		$ro=$tpl->unset_block($ro,array('album_in_cat'));
		$ro = $tpl->assign_vars($ro,
			array(
				'list.TITLE' => $lang['search_result'],
				'list.TOTAL'	=> "<center>{$lang['there_is']} {$tt} {$lang['result']} {$lang['with_keyword']} <b><font color='blue'>{$cmd[4]}</font></b></center>",
				'list.PAGES' => $func->viewpages($tt,$conf['media_per_page'],$cmd[3],$cmd[4]),	
			)
		);
		
		$ro = $tpl->assign_blocks_content($ro,array(
				'list_media'	=>	$html,
			)
		);
		$main_content=$ro;	
		
	}
	if($cmd[2]==2 || $cmd[2]==3){
		if($cmd[2]==2){ //search album
			$q="SELECT * FROM {$conf['prefix']}multimedia WHERE multi_type=1 AND multi_name_ascii LIKE '%".$kw."%' ORDER BY multi_name ASC, multi_name_ascii ASC LIMIT ".$limit.",".$conf['media_per_page'];
			$tt = $DB->num_rows($DB->query("SELECT multi_id FROM ".$conf['prefix']."multimedia WHERE multi_type=1 AND multi_name_ascii LIKE '%".$kw."%'"));
		}
		if($cmd[2]==3){ //search movie
			$q="SELECT * FROM {$conf['prefix']}multimedia WHERE multi_type=2 AND multi_name_ascii LIKE '%".$kw."%' ORDER BY multi_name ASC, multi_name_ascii ASC LIMIT ".$limit.",".$conf['media_per_page'];
			$tt = $DB->num_rows($DB->query("SELECT multi_id FROM ".$conf['prefix']."multimedia WHERE multi_type=2 AND multi_name_ascii LIKE '%".$kw."%'"));
		}
		$ro = $tpl->get_tpl('list_multimedia');
		if ($tt) {
		$t['row'] = $tpl->get_block_from_str($ro,'list_multi.row',1);
		$html = '';
		$qr=$DB->query($q);
		while ($r = $DB->fetch_row($qr)) {
			static $i = 0;
			$tr= (fmod($i,3) == 0)?'</tr><tr>':'';
			$multi_img = $func->get_img('multimedia',$r['multi_id']);
			$tit_ascii=$func->title_2_ascii($r['multi_name_ascii']);
			if($cmd[2]==2){
				$link=$func->change_link("album",$r['multi_id'],$tit_ascii);
				$width="100";
				$height="100";
			}
			elseif($cmd[2]==3){
				$link=$func->change_link("movie",$r['multi_id'],$tit_ascii);
				$width="100";
				$height="150";
			
			}
			$html .= $tpl->assign_vars($t['row'],
				array(
					'multi.IMG'		=>	$multi_img,
					'multi.URL'		=>	$link,
					'multi.NAME'	=>	$r['multi_name'],
					'img.WIDTH'		=> $width,
					'img.HEIGHT'	=> $height,
					'multi.TR'		=> $tr,
					'multi.VIEWED'	=>	$lang['num_viewed']." ".$r['multi_viewed'],
				)
			);
			$i++;
		}

	}
	$result_kw="<center>{$lang['there_is']} {$tt} {$lang['result']} {$lang['with_keyword']} <b><font color='blue'>{$cmd[4]}</font></b></center>";				
		$ro = $tpl->assign_vars($ro,
			array(
				'multimedia.TITLE' => $lang['search_result'],
				'multimedia.TOTAL'	=> $result_kw,
				'multimedia.PAGES' => $func->viewpages($tt,$conf['media_per_page'],$cmd[3],$cmd[4]),
			)
		);
		$ro = $tpl->assign_blocks_content($ro,array(
				'list_multi'	=>	$html,
			)
		);
		$main_content=$ro;	
	}
}
?>
